package com.xlserver.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xlserver.service.AdministratorService;
import com.xlserver.service.MessageService;
import com.xlserver.util.ImageParser;
import com.xlserver.util.SMSUtils;
import com.xlserver.util.model.CounselorInfo;
import com.xlserver.util.model.SupervisorInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Map;

@RestController
@RequestMapping("/administrator")
@AllArgsConstructor
@Api(value = "用户相关模块接口", tags = "用户相关模块接口")
public class AdministratorController {
    @Autowired
    public SMSUtils smsUtils;

    @Autowired
    public ImageParser parser;

    @Autowired
    public AdministratorService administratorService;

    @Autowired
    public MessageService messageService;


    /**
     * 获取所有咨询师
     */
    @GetMapping("getAllCounselors")
    @ApiOperation(value = "获取所有咨询师", notes = "无参")
    public JSONArray getAllCounselors(){
        return administratorService.getAllCounselors();
    }

    /**
     *根据姓名模糊查询咨询师
     */
    @PostMapping("getCounselorByName")
    @ApiOperation(value = "根据姓名模糊查询咨询师", notes = "{name:名字}")
    public JSONArray getCounselorByName(@RequestBody Map<String,String> info){
        String name = info.get("name");
        return administratorService.getCounselorByName(name);
    }
    /**
     * 添加咨询师
     * 咨询师姓名,性别,年龄
     * 身份证号码,电话,邮箱,绑定督导(id),密码
     * 工作单位、个人职称title、值班安排（1234567）
     */
    @PostMapping("addCounselor")
    @ApiOperation(value = "添加咨询师", notes = "counselorInfo")
    public JSONObject addCounselor(@RequestBody CounselorInfo counselorInfo){
        System.out.println(counselorInfo.toString());
        //TODO 新增咨询师
//        System.out.println(counselorInfo.toString());
//        return new JSONObject();
        return administratorService.insertCounselor(counselorInfo);
    }
    /**
     * 修改咨询师信息
     */
    @PostMapping("updateCounselor")
    @ApiOperation(value = "修改咨询师信息", notes = "CounselorInfo,修改需带id")
    public JSONObject updateCounselor(@RequestBody CounselorInfo counselorInfo){
        //TODO
        return administratorService.updateCounselor(counselorInfo);
    }
    /**
     * 查询某个咨询师的相关信息
     */
    @PostMapping("getCounselor")
    @ApiOperation(value = "查询某个咨询师的相关信息", notes = "{id : id入参}")
    public JSONObject getCounselor(@RequestBody Map<String,String> info){
       return administratorService.getCounselor(Integer.parseInt(info.get("id")));
    }
    /**
     * 获取所有督导信息
     */
    @GetMapping("getAllSupervisors")
    @ApiOperation(value = "获取所有督导信息", notes = "无")
    public JSONArray getAllSupervisors(){
        return administratorService.getAllSupervisors();
    }
    /**
     * 模糊查找督导姓名
     */
    @PostMapping("getSupervisorByName")
    @ApiOperation(value = "模糊查找督导姓名", notes = "{name : 名字}")
    public JSONArray getSupervisorByName(@RequestBody Map<String,String> info){
        String name = info.get("name");
        return administratorService.getSupervisorByName(name);
    }

    /**
     * 获取当前所有咨询师正在进行的会话数量
     */
    @GetMapping("getSumOfChats")
    public JSONObject getSumOfChats(){
        return messageService.getSumOfChat();
    }

    /**
     * 获取当前所有督导正在进行的会话数量
     */
    @GetMapping("getSumOfSuperChats")
    public JSONObject getSumOfSuperVisorChats(){
        return messageService.getSumOfSuperChat();
    }

    /**
     *查询督导详细信息
     */
    @PostMapping("detailsOfSupervisor")
    @ApiOperation(value = "督导详细信息", notes = "{id : id入参}")
    public JSONObject getDetailsOfSupervisor(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return administratorService.detailsOfSupervisor(id);
    }
    /**
     * 新增督导
     */
    @PostMapping("addSupervisor")
    @ApiOperation(value = "新增督导", notes = "supervisorInfo")
    public JSONObject addSupervisor(@RequestBody SupervisorInfo supervisorInfo){
        return administratorService.addSupervisor(supervisorInfo);
    }
    /**
     * 修改督导信息
     * 督导id 姓名 性别 年龄 身份证号码 电话 邮箱 绑定的咨询师（id）
     * 密码 工作单位 职称 督导资质 资质编号 周值班安排（1111000）
     */

    @PostMapping("updateSupervisor")
    @ApiOperation(value = "新增督导", notes = "supervisorInfo")
    public JSONObject updateSupervisor(@RequestBody SupervisorInfo supervisorInfo){
        return administratorService.updateSupervisor(supervisorInfo);
    }
    /**
     * 删除咨询师
     */
    @PostMapping("deleteCounselor")
    @ApiOperation(value = "删除咨询师", notes = "{id:id入参}")
    public JSONObject deleteCounselor(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return administratorService.deleteCounselor(id);
    }
    /**
     * 删除督导
     */
    @PostMapping("deleteSupervisor")
    @ApiOperation(value = "删除督导", notes = "{id:id入参}")
    public JSONObject deleteSupervisor(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return administratorService.deleteSupervisor(id);
    }
    /**
     * 获取所有访客
     */
    @GetMapping("getAllUsers")
    @ApiOperation(value = "获取所有访客", notes = "无")
    public JSONArray getAllUsers(){
        return administratorService.getAllUsers();
    }
    /**
     * 姓名查找访客
     */
    @PostMapping("getUserByName")
    @ApiOperation(value = "姓名查找访客", notes = "{name:name入参}")
    public JSONArray getUserByName(@RequestBody Map<String,String> info){
        String name = info.get("name");
        System.out.println("==="+name);
        return administratorService.getUserByName(name);
    }

    /**
     * 禁用访客
     */
    @PostMapping("banUser")
    @ApiOperation(value = "禁用访客", notes = "{id:id入参}")
    public JSONObject banUser(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return administratorService.banUser(id);
    }
    /**
     * 解禁访客
     */
    @PostMapping("unlockUser")
    @ApiOperation(value = "解禁访客", notes = "{id:id入参}")
    public JSONObject unLockUser(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return administratorService.unlockUser(id);
    }
    /**
     * 机构管理员登录
     */
    @PostMapping("login")
    public JSONObject login(@RequestBody Map<String,String> info) throws ParseException {
        String phoneNum = info.get("phoneNum");
        String password = info.get("password");
        return administratorService.getUserInfo(phoneNum,password);
    }
    /**
     * 获取所有会话
     */
    @GetMapping("getAllChats")
    public JSONArray getAllChats(){
        return administratorService.getAllChats();
    }

    /**
     * 机构管理员根据姓名模糊查询获取所有会话
     */
    @PostMapping("getAllChatsByName")
    public JSONArray getChatsByName(@RequestBody Map<String,String> info){
        String name = info.get("name");
        return administratorService.getChatsByName(name);
    }
}
